#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

int main (int argc, char* argv[])
{
  //if(argc!=2)
  //{
  //  return 1;
  //}

  //int fd = open("log.txt", O_CREAT| O_WRONLY|O_TRUNC, 0666);
  //printf("fd:%d\n", fd);
  //dup2(fd,1); //将stdout的内容修改为fd
  //printf("打印屏幕上的内容：%s\n",argv[1]);

  //close(fd);
  //return 0;


  //// 关闭stdin
  //close(0);
  //int fd = open("log.txt", O_RDONLY);
  //if(fd<0)
  //{
  //  perror("open:");
  //  return 1;
  //}
  //printf("fd = %d \n", fd);
  //char buffer[128];
  //fgets(buffer, sizeof buffer, stdin);
  //printf("buffer:%s\n", buffer);
  //close(fd);

  close(1);
  //变成追加方式打开文件
  int fd = open("log.txt", O_WRONLY | O_CREAT | O_APPEND, 0666);

  printf("打开文件分配的fd为%d\n", fd);
  printf("打印到屏幕上的内容\n");
  fprintf(stdout, "hello fprintf\n");
  const char* s1 = "hello fwrite\n";
  fwrite(s1, 1, strlen(s1), stdout);
  
  //fflush(stdout);
 // close(fd);
  return 0;
}


